<?php

namespace app\controller;

use app\BaseController;
use DateTime;
use think\facade\Request;
use think\facade\Db;

class Trade extends BaseController {
    // GET $bookId
    public function fetchbooktrade() {
        $result = [
            "status" => false,
            "description" => "未知错误",
            "trade" => []
        ];

        $bookId = Request::get("bookId");
        
        if($bookId != null) {
            $result["status"] = true;
            $result["description"] = "success";

            $find_result = Db::table("ZZZ_trade_info")
            ->alias("trade")
            ->where("bookId", $bookId)
            ->join("ZZZ_user_info user", "trade.wantuserId = user.userId")
            ->order('trade.timestamp', 'asc')
            ->select()->toArray();

            if($find_result != []) {
                foreach ($find_result as $find_book_key => $find_book) {
                    // 处理imageId和时间戳
                    $now_bookId = $find_book["bookId"];
                    $image_result = Db::table("ZZZ_book_image")
                                    ->where("bookId", $now_bookId)
                                    ->where("valid", 1)
                                    ->order("timestamp")
                                    ->column("imageId");
                    $find_result[$find_book_key]["imageId"] = $image_result;
    
                    $timestamp_datetime = new DateTime($find_book["timestamp"]);
                    $timestamp_integer = $timestamp_datetime->getTimestamp();
                    $find_result[$find_book_key]["timestampInteger"] = $timestamp_integer;
                }
            }

            $result["collection"] = $find_result;
        } else {
            $result["description"] = "请求出错";
        } 
        return json($result);
    }

    // POST $userId $bookId $wantPrice $location $comment
    public function addtrade() {
        $result = [
            "status" => false,
            "description" => "未知错误"
        ];

        $userId = Request::post("userId");
        $bookId = Request::post("bookId");
        $wantPrice = Request::post("wantPrice");
        $location = Request::post("location");
        $comment = Request::post("comment");

        if($userId != null 
        && $bookId != null
        && $wantPrice != null
        && $location != null) {
            $unique_tradeId = "T" . uniqid();
                
            $save_trade_result = Db::table("ZZZ_trade_info")->insert([
                "tradeId" => $unique_tradeId,
                "bookId" => $bookId,
                "wantuserId" => $userId,
                "wantPrice" => $wantPrice,
                "location" => $location,
                "comment" => $comment
            ]);

            if($save_trade_result == 1) {
                $result["status"] = true;
                $result["description"] = "success";
            }
        } else {
            $result["description"] = "请求错误";
        }

        return json($result);
    }
}
